package com.twu.biblioteca;

import java.util.ArrayList;

public class IOHandler implements IOHandlerInterface {

    private final Validator validator;
    private final ReadFromKeyboard reader;

    public IOHandler(Validator validator, ReadFromKeyboard reader) {
        this.validator = validator;
        this.reader = reader;
    }

    public Validator getValidator() {
        return validator;
    }

    @Override
    public String printBookAvailability(Boolean availability) {
        if (availability) return "available";
        return "reserved";
    }

    @Override
    public void printMovie(int index, Movie movie) {
        System.out.format("%-5d%-80s%-10d%n", index, movie.getTitle(), movie.getYear());

    }

    @Override
    public void printMovieDetails(Movie movie) {
        System.out.format("%-80s%-6s%-25s%-10s%n", "Movie", "Year", "Director", "Rating");
        this.printlnMessage("-----------------------------------------------------------------------------------------------------------------------------");
        System.out.format("%-80s%-6s%-25s%-10s%n", movie.getTitle(), movie.getYear(), movie.getDirector(), movie.getRating());
    }

    @Override
    public void printUserDetails(Member user) {
        printlnMessage("Name     : " + user.getName());
        printlnMessage("Email    : " + user.getEmailAddress());
        printlnMessage("Phone nr : " + user.getPhoneNumber());
    }

    public ReadFromKeyboard getReader() {
        return reader;
    }

    @Override
    public void printlnMessage(String message) {
        System.out.println(message);
    }

    @Override
    public void printMessage(String message) {
        System.out.print(message);
    }

    @Override
    public int returnValidInput(int optionListSize) {
        boolean valid = false;
        int input = 0;

        while (!valid) {
            input = reader.readInput();
            try {
                valid = validator.validateChoiceAndPrintWarning(input, optionListSize);
            } catch (InputNotValidException e) {
                printlnMessage(e.getErrorMessage());
            }
        }
        return input;
    }

    @Override
    public void printMenu(ArrayList<String> menu) {
        printlnMessage("\nPlease pick an option:\n");
        printlnMessage(printlnMenuToString(menu));
    }

    @Override
    public String printlnMenuToString(ArrayList<String> menu) {
        String separator = System.getProperty("line.separator");
        String stringExpected = "";

        for (int i = 0; i < menu.size(); i++)
            stringExpected += "[" + (i + 1) + "]" + menu.get(i) + separator;

        return stringExpected;
    }

    @Override
    public void printBookHeader() {
        System.out.format("%-5s%-80s%-25s%-16s%n", "Nr.", "Title", "Author", "Availability");
        this.printlnMessage("-----------------------------------------------------------------------------------------------------------------------------");
    }

    @Override
    public void printBook(int index, Book book) {

        System.out.format("%-5d%-80s%-25s%-16s%n", index, book.getTitle(), book.getAuthor(), this.printBookAvailability(book.getAvailable()));
    }

}
